$(function (){
    $('#search').on('keyup',function (){
        var keyword = $(this).val();
        if (keyword != '' && keyword != null){
            $.ajax({
                type     :'GET',
                url      : 'SearchBorrowInfoByBNServlet?key='+keyword,
                success  : function (data){
                    console.log(data);
                    var productListHTML = '';
                    var length = (data.length < 5)?data.length:5;
                    for(var i = 0; i < length;i++){
                        productListHTML += '<li class=\"productAutoItem\"data-name="';
                        productListHTML += data[i].bookName;
                        productListHTML += '">';
                        productListHTML += data[i].bookName + "  ";
                        productListHTML += '</li>';
                    }
                    $('#productAutoList').html(productListHTML);
                    $('#productAutoComplete').show();
                },
                error    : function (errorMsg){
                    console.log(errorMsg);
                }
            })
        }
        else{
            $('#productAutoComplete').hide();
        }
    });
    $(document).on('click','.productAutoItem',function (){
        $('#productAutoComplete').hide();
        $('#search').val('');
        window.location = 'GetBorrowInfoServlet?current=1&size=10&key='+$(this).data('name');
    })
    $('#bookinfo').on('click',function (){
        // window.location.href = 'http://localhost:8080/final_war_exploded/GetBorrowInfoServlet';
        window.location = 'GetServlet';
    })
});